package com.isoftstone.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * @Description:
 * @Author: guoX
 * @Date: 2019/11/5
 */
public class CodeUtil {

    private static int codeCount = 4;
    private static int w = 80;
    private static int h = 35;

    private static char[] codeSequence = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y',
            'Z','0','1','2','3','4','5','6','7','8','9'};

    private Color color = new Color(255,255,255);

    private String[] fonts = {"宋体","华文楷体","黑体","微软黑体","楷体_GB2312"};
    Random r = new Random();


    public static String generateCode(){
        Random random = new Random();

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < codeCount; i++) {
            String value = String.valueOf(codeSequence[random.nextInt(36)]);
            buffer.append(value);
        }
        return buffer.toString();
    }

    private Color randomColor(){
        int red = r.nextInt(150);
        int green = r.nextInt(150);
        int blue = r.nextInt(150);
        return new Color(red,green,blue);
    }

    private Font randomFont(){
        int index = r.nextInt(fonts.length);
        String font = fonts[index];
        int style = r.nextInt(4);
        int size = r.nextInt(5) + 24;
        return new Font(font,style,size);
    }

    private BufferedImage createImg(){
        BufferedImage bufferedImage = new BufferedImage(75, 35, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(this.color);
        graphics.fillRect(0,0,w,h);
        return bufferedImage;
    }

    private void drawLine(BufferedImage image){
        int num = 3;
        Graphics2D graphics = (Graphics2D)image.getGraphics();
        for (int i = 0; i < num; i++) {
            int x1 = r.nextInt(w);
            int y1 = r.nextInt(h);
            int x2 = r.nextInt(w);
            int y2 = r.nextInt(h);
            graphics.setStroke(new BasicStroke(1.5F));
            graphics.setColor(Color.blue);
            graphics.drawLine(x1,y1,x2,y2);
        }

    }

    public String getImg(OutputStream outputStream){
        BufferedImage img = createImg();
        Graphics2D graphics = (Graphics2D)img.getGraphics();
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < codeCount; i++) {
            String value = String.valueOf(codeSequence[r.nextInt(35)]);
            float x = i * 1.0F * w / 4 ;
            graphics.setFont(randomFont());
            graphics.setColor(randomColor());
            graphics.drawString(value,x,h-5);
            buffer.append(value);
        }

        String string = buffer.toString();
        drawLine(img);

        try {
            ImageIO.write(img,"PNG",outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return string;

    }




    public static void main(String[] args) {

        CodeUtil codeUtil = new CodeUtil();

        System.out.println(String.valueOf(null));
    }

}
